//
//  SLCoreCertificationViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//  
  

#import "SLCoreCertificationViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import <MJRefresh.h>
#import "SLAuthenticationPush.h"

#import "SLCoreCertificationTableViewCell.h"

@interface SLCoreCertificationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy  ) NSMutableDictionary *data;
@end

@implementation SLCoreCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavBar:@"Core Certification"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(0);
        make.top.equalTo(self.navBar.mas_bottom).inset(0);
        make.bottom.equalTo(self.view).inset(0);
    }];
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self requestDataWithCompletion:^(NSDictionary *dict) {
            @strongify(self);
            [self.tableView.mj_header endRefreshing];
            self.data = [dict deepMutableCopy];
            [self.tableView reloadData];
        }];
    }];
    
    // 首次加载地址接口
    [AddressRequestProvider requestAddressWithSuccess:^(NSArray * _Nonnull arr) {
    } failure:^(YTKRequest * _Nonnull request) {
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    @weakify(self);
    [self requestDataWithCompletion:^(NSDictionary *dict) {
        @strongify(self);
        self.data = [dict deepMutableCopy];
        [self.tableView reloadData];
    }];
}

- (void)requestDataWithCompletion:(void(^)(NSDictionary *dict))completion {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ProductDetailRequestProvider requestProductDetailWithProductID:self.productId success:^(NSDictionary * _Nonnull dict) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        !completion ?: completion(dict);
    } failure:^(YTKRequest * _Nonnull request) {
        !completion ?: completion(nil);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [ResponseToast showToastWithRequest:request];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLCoreCertificationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLCoreCertificationTableViewCell" forIndexPath:indexPath];
    NSDictionary *swelling = self.data[@"swelling"];
    cell.productCard.productName.text = swelling[@"chair"];
    cell.productCard.creditLimitMoney.text = swelling[@"loveliest"];
    
    @weakify(self);
    [cell.productCard.apply jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
        [SLAuthenticationPush pushStartAuthenticationPageWithDict:self.data view:self.view];
    }];
    [cell.start.startAuthentication jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
        [SLAuthenticationPush pushStartAuthenticationPageWithDict:self.data view:self.view];
    }];
    
    NSArray *favorably = self.data[@"favorably"];
    NSInteger currentStep = favorably.count;
    for (NSDictionary *dict in favorably) {
        if ([dict[@"contained"] sl_intValue] == 0) {
            currentStep = [favorably indexOfObject:dict];
            break;
        }
    }
    cell.list.progressView.stepCount = favorably.count;
    cell.list.progressView.currentStep = currentStep;
    cell.list.items = favorably;
    cell.list.itemClickCallback = ^(NSInteger index) {
        // 点击的是item
        @strongify(self);
        [SLAuthenticationPush pushAuthenticationItemPageWithDict:self.data index:index view:self.view];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:SLCoreCertificationTableViewCell.class forCellReuseIdentifier:@"SLCoreCertificationTableViewCell"];
    }
    return _tableView;
}


@end
